
var unameFlag = true;
var upwFlag = true;
var uemailFlag = true;

$(document).ready(function(){
	
	initInnerSpan();
	//注册
	initUpdateUserName();
	initUpdatePw();
	initUpdateEmail();
	initUpdateSubmit();
});

function initInnerSpan(){
	$("#span1").click(function(){
		$(this).css("visibility","hidden");
		$("#pw1").focus();
	});
	$("#pw1").focus(function(){
		$("#span1").css("visibility","hidden");
	});
	
	$("#span2").click(function(){
		$(this).css("visibility","hidden");
		$("#pw2").focus();
	});
	
	$("#pw2").focus(function(){
		$("#span2").css("visibility","hidden");
	});
	
	$("#span3").click(function(){
		$(this).css("visibility","hidden");
		$("#pw3").focus();
	});
	$("#pw3").focus(function(){
		$("#span3").css("visibility","hidden");
	});
}

function initUpdateSubmit(){
	$("#saveSubmit").click(function(){
		
		//未改动时不保存操作，不提醒密码
		if($("#usernameText").val()==$("#puserName").val() 
				&& $("#pemail").val()==$("#userEmailText").val()
				&& $("#pw2").val()=="" && $("#pw3").val()==""){
			return;
		}
		
		if($("#pw1").val()==""){
			showErr("msgSpan1","请输入密码");
			return;
		}
		
		console.log(unameFlag+" "+upwFlag+" "+uemailFlag);
		
		if(unameFlag && upwFlag && uemailFlag){
			
			$.ajax({
		        type: "post",
		        url: $('#basePath').val()+"updateUser",
		        data: {
		        	"user.id":$("#pid").val(),
		        	"user.username":$("#usernameText").val(),
		        	"user.password":$("#pw1").val(),
		        	"user.email":$("#userEmailText").val(),
		        	"newPs":$("#pw2").val()
		        },
		        success:function(data,status) {
		        	
		        	if(data=="success"){
		        		showCorrect("saveSpan","修改成功");
		        	}else{
		        		showErr("saveSpan","填写有误或发生未知错误");
		        	}
		        },
		        error:function(data,status){
		        	alert("ajax error.");
		        }
		     });
		}else{
			//validate update message 
			validateUpdateMsg();
		}
	});
}

function initUpdateUserName(){
	
	//确定用户名的正则表达式：长度为2-16，不包含特殊字符
	var userNamePattren =  /^[\u4e00-\u9fa5\w]{2,16}$/ ;	
	var oldName = $("#puserName").val();
	
	$("#usernameText").blur(function(){
		
		var name = $("#usernameText").val();
		
		if(name != "" && name!=oldName){
			
			if(userNamePattren.test(name)){
				unameFlag = true;
			}else{
				showErr("nameValidMsg","长度为2-20，不能包含特殊字符");
				unameFlag = false;
			}
			//到数据库进行验证，要求姓名必须唯一
			if(unameFlag){
				$.ajax({
			        type: "post",
			        url: $('#basePath').val()+"checkUserName?user.username="+encodeURI(encodeURI($("#usernameText").val())),
			        data: {},
			        success:function(data,status) {
			        	if(data.isExist){
			        		showErr("nameValidMsg","此用户名已被占用");
			        		unameFlag = false;
			        	}else{
			        		showCorrect("nameValidMsg","√");
			        		unameFlag = true;
			        	}
			        },
			        error:function(data,status){
			        	alert("ajax error.");
			        	unameFlag = false;
			        }
			     });
			}
		}else if(name==""){
    		$("#nameValidMsg").css("visibility","hidden");
    		unameFlag = false;
		}else{
    		$("#nameValidMsg").css("visibility","hidden");
    		unameFlag = true;
		}
	});
}

function initUpdatePw(){
	var pwPat = /^(\w){6,20}$/;			//密码正则表达式
	
	$("#pw1").blur(function(){			//在后台验证密码
		var pw = $(this).val();
		
		if(pw != ""){
			$.ajax({
		        type: "post",
		        url: $('#basePath').val()+"checkPs",
		        data: {"user.password":pw},
		        success:function(data,status) {
		        	if(data=="right"){
		        		showCorrect("msgSpan1","√");
		        		upwFlag = true;
		        	}else{
		        		showErr("msgSpan1","密码不正确");
		        		upwFlag = false;
		        	}
		        },
		        error:function(data,status){
		        	alert("ajax error.");
		        	upwFlag = false;
		        }
		     });
		}else{
			$("#span1").css("visibility","visible");
			$("#msgSpan1").css("visibility","hidden");
			upwFlag = false;
		}
		
	});
	
	
	$("#pw2").blur(function(){
		var pw = $(this).val();
		
		if(pw != ""){
			if(!pwPat.test(pw)){
				showErr("msgSpan2","请输入6-20位字母数字或下划线");
				upwFlag = false;
			}else{
        		$("#msgSpan2").css("visibility","hidden");
        		upwFlag = true;
			}
		}else{
			$("#span2").css("visibility","visible");
			$("#msgSpan2").css("visibility","hidden");
			upwFlag = false;
		}
		
	});
	
	$("#pw3").blur(function(){
		var pw2 = $("#pw2").val();
		var pw3 = $(this).val();
		var pw2vis = $("#msgSpan2").css("visibility");
		
		//pw2显示错误信息时，pw3不进行错误提示
		
		if(pw3==""){
			$("#span3").css("visibility","visible");
			upwFlag = false;
			return;
		}
		
		if(pw2 != pw3 && pw2vis != "visible"){
			showErr("msgSpan3","两次输入密码不一致");
			upwFlag = false;
		}else if(pw2 != pw3){
			upwFlag = false;
		}else{
			$("#msgSpan3").css("visibility","hidden");
			upwFlag = true;
		}
	});
}

function initUpdateEmail(){
	$("#userEmailText").blur(function(){
		var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ ;
		var email = $(this).val();
		
		var oldEmail = $("#pemail").val();
		
		if(email != "" && email!=oldEmail){
			if(re.test(email)){
	    		uemailFlag = true;
			}else{
				showErr("EmlValidMsg","邮箱格式不正确");
	    		uemailFlag = false;
			}
			
			//到数据库进行验证，要求邮箱必须唯一
			if(uemailFlag){
				$.ajax({
			        type: "post",
			        url: $('#basePath').val()+"checkEmail?user.email="+$("#userEmailText").val(),
			        data: {},
			        success:function(data,status) {
			        	if(data.isExist){
			        		showErr("EmlValidMsg","此邮箱已被注册");
			        		uemailFlag = false;
			        	}else{
			        		showCorrect("EmlValidMsg","√");
			        		uemailFlag = true;
			        	}
			        },
			        error:function(data,status){
			        	alert("ajax error.");
			        }
			     });
			}
		}else{
			uemailFlag = true;
			$("#EmlValidMsg").css("visibility","hidden");
		}
	});
}

function validateUpdateMsg(){
	//usernameText pw1 pw2 userEmailText securityCode
	
	if($("#usernameText").val()==""){
		showErr("nameValidMsg","输入用户名");
	}
	
	if($("#pw2").val()!="" && $("#pw3").val()==""){
		showErr("msgSpan3","请再输入密码");
	}
	
	var msgSpanCss = $("#EmlValidMsg").css("visibility");
	
	if(!uemailFlag && msgSpanCss=="hidden"){
		showErr("EmlValidMsg","邮箱格式不正确");
	}
}

function showCorrect(id,msg){
	$("#"+id).text(msg);
	$("#"+id).removeClass("errorMessage").addClass("correctMessage");
	$("#"+id).css("visibility","visible");
}

function showErr(id,msg){
	$("#"+id).text(msg);
	$("#"+id).removeClass("correctMessage").addClass("errorMessage");
	$("#"+id).css("visibility","visible");
}

